﻿#region 头注释
/******************************************************************************
文件名称    :   SurgeryKidney.cs
创建日期    :   2011/5/11 22:22:11
功能描述    :   SurgeryKidney实体类
修改日志    :   
    2011-05-11 22:22  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;
using Dare.Utilities;

namespace Stone.Components.Entities
{
    /// <summary>
    /// SurgeryKidney实体类
    /// </summary>
    public class SurgeryKidney : EntityBase<SurgeryKidney, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public SurgeryKidney()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            channel = null;
            channelSize = null;
            kidneyEnter = (KidneyEntranceEnum)0;
            kidneyBlood = null;
            treat = null;
            dSARecordFile = null;
            dSARecordFileId = null;
            surgeryRecordFile = null;
            surgeryRecordFileId = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override SurgeryKidney CreateNew()
        {
            return new SurgeryKidney();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.SurgeryKidney; }
        }

        /// <summary>
        /// 获取或设置通道
        /// </summary>
        public int? Channel
        {
            get { return channel; }
            set
            {
                if (entityState == EntityState.Saved && channel != value) entityState = EntityState.Changed;
                channel = value;
            }
        }
        protected int? channel;

        /// <summary>
        /// 获取或设置通道大小
        /// </summary>
        public int? ChannelSize
        {
            get { return channelSize; }
            set
            {
                if (entityState == EntityState.Saved && channelSize != value) entityState = EntityState.Changed;
                channelSize = value;
            }
        }
        protected int? channelSize;

        /// <summary>
        /// 获取或设置肾盏入路
        /// </summary>
        public KidneyEntranceEnum? KidneyEnter
        {
            get { return kidneyEnter; }
            set
            {
                if (entityState == EntityState.Saved && kidneyEnter != value) entityState = EntityState.Changed;
                kidneyEnter = value;
            }
        }
        protected KidneyEntranceEnum? kidneyEnter;

        /// <summary>
        /// 获取或设置是否肾出血
        /// </summary>
        public bool? KidneyBlood
        {
            get { return kidneyBlood; }
            set
            {
                if (entityState == EntityState.Saved && kidneyBlood != value) entityState = EntityState.Changed;
                kidneyBlood = value;
            }
        }
        protected bool? kidneyBlood;

        /// <summary>
        /// 获取或设置治疗方式(0:保守;1:开放)
        /// </summary>
        public bool? Treat
        {
            get { return treat; }
            set
            {
                if (entityState == EntityState.Saved && treat != value) entityState = EntityState.Changed;
                treat = value;
            }
        }
        protected bool? treat;

        /// <summary>
        /// 获取或设置DSA录像文件
        /// </summary>
        public MediaFile DSARecordFile
        {
            get { return dSARecordFile; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != dSARecordFileId) entityState = EntityState.Changed;
                dSARecordFile = value;
            }
        }
        protected MediaFile dSARecordFile;

        /// <summary>
        /// 获取或设置DSA录像文件
        /// </summary>
        public long? DSARecordFileId
        {
            get { return dSARecordFile == null ? dSARecordFileId : dSARecordFile.Id; }
            set { dSARecordFileId = value; }
        }
        protected long? dSARecordFileId;

        /// <summary>
        /// 获取或设置手术录像文件
        /// </summary>
        public MediaFile SurgeryRecordFile
        {
            get { return surgeryRecordFile; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != surgeryRecordFileId) entityState = EntityState.Changed;
                surgeryRecordFile = value;
            }
        }
        protected MediaFile surgeryRecordFile;

        /// <summary>
        /// 获取或设置手术录像文件
        /// </summary>
        public long? SurgeryRecordFileId
        {
            get { return surgeryRecordFile == null ? surgeryRecordFileId : surgeryRecordFile.Id; }
            set { surgeryRecordFileId = value; }
        }
        protected long? surgeryRecordFileId;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override SurgeryKidney CopyTo(SurgeryKidney entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.channel = this.channel;
            entity.channelSize = this.channelSize;
            entity.kidneyEnter = this.kidneyEnter;
            entity.kidneyBlood = this.kidneyBlood;
            entity.treat = this.treat;
            entity.dSARecordFile = this.dSARecordFile;
            entity.dSARecordFileId = this.dSARecordFileId;
            entity.surgeryRecordFile = this.surgeryRecordFile;
            entity.surgeryRecordFileId = this.surgeryRecordFileId;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<SurgeryKidney>
        {
            SurgeryKidney entity;
            public DefaultXmlSerializer(SurgeryKidney entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public SurgeryKidney Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "Channel":
                        entity.channel = XmlConvert.ToInt32(reader.ReadElementString());
                        break;
                    case "ChannelSize":
                        entity.channelSize = XmlConvert.ToInt32(reader.ReadElementString());
                        break;
                    case "KidneyEnter":
                        entity.kidneyEnter = (KidneyEntranceEnum)Enum.Parse(typeof(KidneyEntranceEnum), reader.ReadElementString(), true);
                        break;
                    case "KidneyBlood":
                        entity.kidneyBlood = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "Treat":
                        entity.treat = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "DSARecordFileId":
                        entity.dSARecordFileId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "SurgeryRecordFileId":
                        entity.surgeryRecordFileId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                writer.WriteElementString("KidneyEnter", entity.KidneyEnter.ToString());
                if (entity.Channel != null) writer.WriteElementString("Channel", XmlConvert.ToString(entity.Channel.Value));
                if (entity.ChannelSize != null) writer.WriteElementString("ChannelSize", XmlConvert.ToString(entity.ChannelSize.Value));
                //if (entity.KidneyEnter != null) writer.WriteElementString("KidneyEnter", XmlConvert.ToString(entity.KidneyEnter.Value));
                if (entity.KidneyBlood != null) writer.WriteElementString("KidneyBlood", XmlConvert.ToString(entity.KidneyBlood.Value));
                if (entity.Treat != null) writer.WriteElementString("Treat", XmlConvert.ToString(entity.Treat.Value));
                if (entity.DSARecordFileId != null) writer.WriteElementString("DSARecordFileId", XmlConvert.ToString(entity.DSARecordFileId.Value));
                if (entity.SurgeryRecordFileId != null) writer.WriteElementString("SurgeryRecordFileId", XmlConvert.ToString(entity.SurgeryRecordFileId.Value));
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<SurgeryKidney>
        {
            SurgeryKidney entity;
            public DefaultJsonSerializer(SurgeryKidney entity)
                : base()
            {
                this.entity = entity;
            }

            public SurgeryKidney Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "channel":
                        entity.channel = DbConvert.ToNullableInt32(propValue) ?? entity.channel;
                        break;
                    case "channelSize":
                        entity.channelSize = DbConvert.ToNullableInt32(propValue) ?? entity.channelSize;
                        break;
                    case "kidneyEnter":
                        entity.kidneyEnter = (KidneyEntranceEnum)(DbConvert.ToNullableInt32(propValue) ?? (int)entity.kidneyEnter);
                        break;
                    case "kidneyBlood":
                        entity.kidneyBlood = DbConvert.ToNullableBoolean(propValue) ?? entity.kidneyBlood;
                        break;
                    case "treat":
                        entity.treat = DbConvert.ToNullableBoolean(propValue) ?? entity.treat;
                        break;
                    case "dSARecordFileId":
                        entity.dSARecordFileId = DbConvert.ToNullableInt64(propValue) ?? entity.dSARecordFileId;
                        break;
                    case "surgeryRecordFileId":
                        entity.surgeryRecordFileId = DbConvert.ToNullableInt64(propValue) ?? entity.surgeryRecordFileId;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //通道
                writer.WritePropertyName("channel");
                if (entity.Channel == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Channel.Value);
                }

                //通道大小
                writer.WritePropertyName("channelSize");
                if (entity.ChannelSize == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.ChannelSize.Value);
                }

                //肾盏入路
                writer.WritePropertyName("kidneyEnter");
                writer.WriteValue((int)entity.KidneyEnter);
                //if (entity.KidneyEnter == null)
                //{
                //    writer.WriteNull();
                //}
                //else
                //{
                //    writer.WriteValue(entity.KidneyEnter.Value);
                //}

                //是否肾出血
                writer.WritePropertyName("kidneyBlood");
                if (entity.KidneyBlood == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.KidneyBlood.Value);
                }

                //治疗方式(0:保守;1:开放)
                writer.WritePropertyName("treat");
                if (entity.Treat == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Treat.Value);
                }

                //DSA录像文件
                writer.WritePropertyName("dSARecordFileId");
                if (entity.DSARecordFileId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.DSARecordFileId.Value);
                }

                //手术录像文件
                writer.WritePropertyName("surgeryRecordFileId");
                if (entity.SurgeryRecordFileId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.SurgeryRecordFileId.Value);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }

    /// <summary>
    /// 肾盏入路枚举
    /// </summary>
    public enum KidneyEntranceEnum
    {
        [EnumDisplayName("上")]
        Up = 0,
        [EnumDisplayName("中")]
        Mid,
        [EnumDisplayName("下")]
        Down,
        [EnumDisplayName("上中")]
        UpMid,
        [EnumDisplayName("中下")]
        MidDown,
        [EnumDisplayName("上下")]
        UpDown,
        [EnumDisplayName("上中下")]
        UpMidDown
    }
}